import pickle

from app_detail.waihui_track.model_node import ShouhuiAccount
from my_tools.path_utils import project_root_path, my_path_join

serialization_path = my_path_join(project_root_path, "out_print/graph_serialization/")


def get_serialization_file_path(shouhui_account: ShouhuiAccount, output_folder=None):
    if output_folder is not None:
        my_serialization_path = my_path_join(output_folder, "graph_serialization")
    else:
        my_serialization_path = serialization_path
    file_name = f"{shouhui_account.get_file_name()}.pkl"
    return my_path_join(my_serialization_path, file_name)


def graph_serialization(tree_node, file_path):
    with open(file_path, 'wb') as output:
        pickle.dump(tree_node, output, pickle.HIGHEST_PROTOCOL)


def graph_deserialization(file_path):
    with open(file_path, 'rb') as input:
        loaded_root = pickle.load(input)
    return loaded_root
